package GUI;

import 试题和试卷.question;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;

import 试题和试卷.*;

public class definitionQuestionItem extends questionItem{
    JRadioButton rightBut,wrongBut;
    JLabel questionInfo,question,answer;
    JScrollPane qScroller;
    int n;
    public  definitionQuestionItem(int n, 试题和试卷.question currentquestion){
        this.n=n;

        this.index=n-1;
        skipButton.setName(""+n);
        String qContent=currentquestion.getText();

        questionInfo = new JLabel("第"+n+"题  判断题");
        question = new JLabel("题目：");
        answer = new JLabel("解答：");

        Font bigFont = new Font("sanserif",Font.BOLD,24);

        rightBut = new JRadioButton("正确");
        wrongBut = new JRadioButton("错误");

        JTextArea questionContent = new JTextArea(6,20);
        questionContent.setText(qContent);
        questionContent.setLineWrap(true);
        questionContent.setWrapStyleWord(true);
        questionContent.setFont(bigFont);
        questionContent.setEditable(false);

        qScroller = new JScrollPane(questionContent);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        questionInfo.setBounds(70,10,300,20);
        qScroller.setBounds(70,40,500,300);
        question.setBounds(20,40,50,30);
        rightBut.setBounds(70,390,200,30);
        wrongBut.setBounds(70,460,200,30);


        rightBut.addActionListener(new rightListener());
        wrongBut.addActionListener(new wrongListener());


        answerSheet.buttons.add(new JButton(""+n));
        setButton(answerSheet.buttons.get(n));
        int defIndex=n-answerSheet.selectionQuestionNum;
        int defRow=defIndex/4+1;
        int defCol=defIndex%4;
        if (defIndex%4==0){
            defRow--;
            defCol=4;
        }
        answerSheet.buttons.get(n).setBounds(50+(defCol-1)*50,240+(answerSheet.selectionRow-1)*40+(defRow-1)*50,40,40);
        answerSheet.mainPanel.add(answerSheet.buttons.get(n));
        answerSheet.mainPanel.updateUI();

        answerSheet.buttons.get(n).addActionListener(new skipListener());
    }

    public class rightListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(rightBut.isSelected()){
                test.resultSet[1][index]="right";
                wrongBut.setSelected(false);
            }
        }
    }

    public class wrongListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(wrongBut.isSelected()){
                test.resultSet[1][index]="wrong";
                rightBut.setSelected(false);
            }
        }
    }

    public void display(){
        test.mainPanel.add(this.rightBut);
        test.mainPanel.add(this.wrongBut);
        test.mainPanel.add(this.questionInfo);
        test.mainPanel.add(this.question);
        test.mainPanel.add(this.answer);
        test.mainPanel.add(this.qScroller);
    }

    public void remove(){
        test.mainPanel.remove(this.rightBut);
        test.mainPanel.remove(this.wrongBut);
        test.mainPanel.remove(this.questionInfo);
        test.mainPanel.remove(this.question);
        test.mainPanel.remove(this.answer);
        test.mainPanel.remove(this.qScroller);
    }

    public void loadAnswer(){
        if(test.resultSet[1][index]!=null&&test.resultSet[1][index].equals("right")){
            rightBut.setSelected(true);
            wrongBut.setSelected(false);
        }else if(test.resultSet[1][index]!=null&&test.resultSet[1][index].equals("wrong")){
            rightBut.setSelected(false);
            wrongBut.setSelected(true);
        }
    }
    public void setButton(JButton button){
        button.setSize(40,40);
        button.setMargin(new Insets(0,0,0,0));
        button.setFont(new Font("宋体",Font.BOLD,14));
        button.setBackground(Color.lightGray);
    }
    public class skipListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){

            test.demo.dispose();
            test.demo=new test(n);
            test.mainPanel.updateUI();


        }
    }

}
